package com.collect.cameroon.zhiHelper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;

import com.collect.cameroon.zhiGlobal.zhiAppSettings.ZHIProjectConfigs;
import com.collect.cameroon.zhiGlobal.zhiAppSettings.ZHIProjectConstants;
import com.collect.cameroon.zhiGlobal.zhiHandler.ZHIProgressHUDHelper;
import com.collect.cameroon.zhiUtils.SPUtils;
import com.collect.cameroon.zhiUtils.StringUtil;

@SuppressLint("StaticFieldLeak")
public class ZHIPhoneCallHelper {
    private static String TAG = "ZHIPhoneCallHelper";

    private ZHIProgressHUDHelper mZHIProgressHUDHelper;

    private static boolean mNxcallInit;
    private static boolean mNxcallLogin;
    private static boolean mNxcallWhenLoginFinish;

    private static String mAccount;
    private static String mPassword;

    private static String mMobile;
    private static String mOrderId;
    private static int mFromType;
    private Context mContext;

    public static ZHIPhoneCallHelper getInstance() {
        return instance;
    }

    private static final ZHIPhoneCallHelper instance = new ZHIPhoneCallHelper();

    public ZHIPhoneCallHelper() {
        mNxcallLogin = false;
        mNxcallWhenLoginFinish = false;
        mAccount = StringUtil.safeString(SPUtils.getInstance().getString(ZHIProjectConstants.NX_ACCOUNT));
        mPassword = StringUtil.safeString(SPUtils.getInstance().getString(ZHIProjectConstants.NX_PASSWORD));
    }

    public void initPhoneCall(Context context, int fromType) {
        try {
            if (context == null) {
                return;
            }

            mFromType = fromType;

            if (ZHIProjectConfigs.IS_NX_CALL) {
                if (!mNxcallInit) {
                    //NxcallSdkUtil.getInstance(context).setNxcallListener(this).init((Activity) context, null, null);
                    mNxcallInit = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void startPhoneCall(Context context, String mobile, String orderId) {
        try {
            if (context == null) {
                return;
            }

            if (TextUtils.isEmpty(orderId)) {
                orderId = "test_nx";
            }

            mMobile = mobile;
            mOrderId = orderId;
            mContext = context;

//            if (ZHIProjectConfigs.IS_NX_CALL) {
//                if (mFromType == ZHIPhoneCallType.KFD0) {
//                    if (ZHIProjectConfigs.IS_NX_CALL_KF) {
//                        showNxPhoneCallAlter(context, mobile, orderId);
//                    } else {
//                        ZHISysShareHelper.callPhone(context, mobile);
//                    }
//                } else if (mFromType == ZHIPhoneCallType.CS) {
//                    if (ZHIProjectConfigs.IS_NX_CALL_CS) {
//                        showNxPhoneCallAlter(context, mobile, orderId);
//                    } else {
//                        ZHISysShareHelper.callPhone(context, mobile);
//                    }
//                } else {
//                    ZHISysShareHelper.callPhone(context, mobile);
//                }
//            } else {
//                ZHISysShareHelper.callPhone(context, mobile);
//            }

            ZHISysShareHelper.callPhone(context, mobile);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stopPhoneCall(Context context) {
        try {
            mNxcallLogin = false;

            if (context == null) {
                return;
            }
            //if (ZHIProjectConfigs.IS_NX_CALL) {
                //NxcallSdkUtil.getInstance(context).onDestroy();
            //}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @Override
//    public void nxcallLoginState(boolean isOk, String msg) {
//        Log.d(TAG, "nxcallLoginState: " + isOk + ", msg: " + msg);
//        mNxcallLogin = isOk;
//        if (mNxcallLogin && mNxcallWhenLoginFinish) {
//            //拨打电话
//            new Handler().postDelayed(this::startNxCall,3000);
//        }
//    }
//
//    @Override
//    public void nxcallConnectState(boolean isOk, String msg) {
//        Log.d(TAG, "nxcallConnectState: " + isOk + ", msg: " + msg);
//
//    }
//
//    @Override
//    public void nxcallCallActive(String phoneActive) {
//        Log.d(TAG, "nxcallCallActive: " + phoneActive);
//    }
//
//    @Override
//    public void nxcallCallDisconnected(String phoneDis) {
//        Log.d(TAG, "nxcallCallDisconnected: " + phoneDis);
//    }

//    private void startNxCall() {
//        //拨打电话
//        dismissProgressHUD();
//        int state = NxcallSdkUtil.getInstance(mContext).nxcall(mMobile, mOrderId);
//        switch (state) {
//            case NxcallStateCode.NXCALL_SUCCESS:
//                //ToastUtil.s("拨号成功");
//                Log.d(TAG, "nxcallLoginState: 拨号成功");
//                break;
//            case NxcallStateCode.NXCALL_LOGIN_ERROR:
//                //ToastUtil.s("未登录");
//                Log.d(TAG, "nxcallLoginState: 未登录");
//                break;
//            case NxcallStateCode.NXCALL_SIP_ERROR:
//                //ToastUtil.s("SIP未链接");
//                Log.d(TAG, "nxcallLoginState: SIP未链接");
//                break;
//            case NxcallStateCode.NXCALL_ACTIVE_ERROR:
//                //ToastUtil.s("正在通话中");
//                Log.d(TAG, "nxcallLoginState: 正在通话中");
//                break;
//            case NxcallStateCode.NXCALL_PHONE_ERROR:
//                //ToastUtil.s("拨号异常");
//                Log.d(TAG, "nxcallLoginState: 拨号异常");
//                break;
//            case NxcallStateCode.NXCALL_OTHER_ERROR:
//                //ToastUtil.s("其他未知错误");
//                Log.d(TAG, "nxcallLoginState: 其他未知错误");
//                break;
//            default:
//                break;
//        }
//    }

//    private void showNxPhoneCallAlter(Context context, String mobile, String orderId) {
//        ZHIDialogNxcallLogin.showDialog(context, null, mAccount, mPassword, new ZHIDialogNxcallLoginListener() {
//            @Override
//            public void onConfirmClick(String accunt, String password) {
//                Log.d(TAG, "mAccount: " + accunt + ", mPassword: " + password + ", mobile: " + mobile);
//
//                try {
//                    if (TextUtils.isEmpty(accunt) || TextUtils.isEmpty(password)) {
//                        return;
//                    }
//
//                    String lastAccount = StringUtil.safeString(mAccount);
//                    String lastPassword = StringUtil.safeString(mPassword);
//
//                    //如果是同一个账号密码，直接打电话
//                    if (mNxcallLogin && accunt.equalsIgnoreCase(lastAccount) && password.equalsIgnoreCase(lastPassword)) {
//                        //拨打电话
//                        startNxCall();
//                        return;
//                    }
//
//                    //缓存
//                    mAccount = accunt;
//                    mPassword = password;
//                    SPUtils.getInstance().put(ZHIProjectConstants.NX_ACCOUNT, mAccount, true);
//                    SPUtils.getInstance().put(ZHIProjectConstants.NX_PASSWORD, mPassword, true);
//
//                    //登录
//                    mNxcallLogin = false;
//                    mNxcallWhenLoginFinish = true;
//                    NxcallSdkUtil.getInstance(context).nxLogin(accunt, password);
//
//                    //HUD
//                    mZHIProgressHUDHelper = new ZHIProgressHUDHelper(context, null, false);
//                    showProgressHUD();
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    dismissProgressHUD();
//                }
//            }
//
//            @Override
//            public void onCancelClick() {
//
//            }
//        });
//    }

    private void showProgressHUD() {
        if (mZHIProgressHUDHelper != null) {
            mZHIProgressHUDHelper.show();
        }
    }

    private void dismissProgressHUD() {
        if (mZHIProgressHUDHelper != null) {
            mZHIProgressHUDHelper.hide();
            mZHIProgressHUDHelper = null;
        }
    }

    public interface ZHIPhoneCallType {
        int KFD0 = 0;
        int CS = 1;
    }
}
